#include "sfsis.h"
#include "mwl_rgbstrip_map.h"
#include "debug.h"
#define I_DMA_RAM_SIZE (12 * 255) //max 255 led 12Byte to One RGB
typedef void (*iColorSequenceFunc)(su8);
iColorSequenceFunc iRGBMap[6];
iColorSequenceFunc iRGBMap_Reverse[6];
MWL_RgbStrip rgbStrip;
su16 i_colorUpdateTimestamp = 0;
SF_Boolen i_isColorTimestampEnable = SF_False;
static unsigned char i_dma[I_DMA_RAM_SIZE];
void i_RGB(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].R;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}

void i_RGB_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].R;
        memory = &i_dma[I_DMA_RAM_SIZE - (origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}

void i_RBG(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].R;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_RBG_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].R;
        memory = &i_dma[I_DMA_RAM_SIZE - (origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_BGR(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].B;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}

void i_BGR_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].B;
        memory = &i_dma[I_DMA_RAM_SIZE - (origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_BRG(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].B;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}

void i_BRG_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].B;
        memory = &i_dma[I_DMA_RAM_SIZE-(origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].G;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_GBR(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].G;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}

void i_GBR_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].G;
        memory = &i_dma[I_DMA_RAM_SIZE - (origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_GRB(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].G;
        memory = &i_dma[origin * 12];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


void i_GRB_Reverse(su8 start)
{
    su8 origin = 0;
    unsigned char val = 0;
    unsigned char *memory;
    unsigned int ii = 0;
    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        origin = ii + start;
        //dot R
        val = rgbStrip.colors[ii].G;
        memory = &i_dma[I_DMA_RAM_SIZE - (origin * 12)];
        memory[0] = mwl_rgbstrip_map[val][0];
        memory[1] = mwl_rgbstrip_map[val][1];
        memory[2] = mwl_rgbstrip_map[val][2];
        memory[3] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].R;
        memory[4] = mwl_rgbstrip_map[val][0];
        memory[5] = mwl_rgbstrip_map[val][1];
        memory[6] = mwl_rgbstrip_map[val][2];
        memory[7] = mwl_rgbstrip_map[val][3];
        val = rgbStrip.colors[ii].B;
        memory[8] = mwl_rgbstrip_map[val][0];
        memory[9] = mwl_rgbstrip_map[val][1];
        memory[10] = mwl_rgbstrip_map[val][2];
        memory[11] = mwl_rgbstrip_map[val][3];
    }
}


/*配置初始化*/
void mwl_rs_init(void)
{
    unsigned int ii = 0;

    for (ii = 0; ii < MWL_RGBSTRIP_DOTS; ii++)
    {
        rgbStrip.colors[ii].R = 0;
        rgbStrip.colors[ii].G = 0;
        rgbStrip.colors[ii].B = 0;
    }

    iRGBMap[CONFIG_FACTORY_RGBTYPE_RGB] = i_RGB;
    iRGBMap[CONFIG_FACTORY_RGBTYPE_RBG] = i_RBG;
    iRGBMap[CONFIG_FACTORY_RGBTYPE_BGR] = i_BGR;
    iRGBMap[CONFIG_FACTORY_RGBTYPE_BRG] = i_BRG;
    iRGBMap[CONFIG_FACTORY_RGBTYPE_GBR] = i_GBR;
    iRGBMap[CONFIG_FACTORY_RGBTYPE_GRB] = i_GRB;

    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_RGB] = i_RGB_Reverse;
    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_RBG] = i_RBG_Reverse;
    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_BGR] = i_BGR_Reverse;
    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_BRG] = i_BRG_Reverse;
    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_GBR] = i_GBR_Reverse;
    iRGBMap_Reverse[CONFIG_FACTORY_RGBTYPE_GRB] = i_GRB_Reverse;

    i_isColorTimestampEnable = SF_True;
}

void mwl_rs_update(void)
{
    if (hwl_dma_spi_isTransferBusy() == SF_False)
    {
        if(gCfgFactoryPtr->isReverse)
        {
            iRGBMap_Reverse[gCfgFactoryPtr->rgbType](gCfgFactoryPtr->logicOriginPoint);
        }
        else
        {
            iRGBMap[gCfgFactoryPtr->rgbType](gCfgFactoryPtr->logicOriginPoint);
        }
        
    }
}

void mwl_rs_flush(void)
{
    if (hwl_dma_spi_isTransferBusy() == SF_False)
    {
        hwl_dma_spi_transfer(i_dma, sizeof(i_dma));
    }
}
